local sk__quanqing = fk.CreateSkill {

  name = "sk__quanqing",

  tags = {},

}



sk__quanqing:addEffect("active", {
  name = "sk__quanqing",
  anim_type = "control",
  card_num = 1,
  target_num = 1,
  prompt = "#sk__quanqing",
  can_use = function(self, player)
    return not player:isKongcheng()
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and Fk:currentRoom():getCardArea(to_select) == Player.Hand and
        not table.contains(player:getTableMark("sk__quanqing_cards-phase"), to_select)
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player and
        not table.contains(player:getTableMark("sk__quanqing_players-phase"), to_select.id)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local mark = player:getTableMark("sk__quanqing_players-phase")
    table.insert(mark, target.id)
    room:setPlayerMark(player, "sk__quanqing_players-phase", mark)
    mark = player:getTableMark("sk__quanqing_cards-phase")
    table.insert(mark, effect.cards[1])
    room:setPlayerMark(player, "sk__quanqing_cards-phase", mark)
    local num = Fk:getCardById(effect.cards[1]).number
    player:showCards(effect.cards)
    if target.dead then return end
    local pattern = ".|14"
    if num < 13 then
      pattern = ".|" .. tostring(num + 1) .. "~13"
    end
    if target:isNude() or
        #room:askForDiscard(target, 1, 1, true, sk__quanqing.name, true, pattern, "#sk__quanqing-discard:" .. player.id .. "::" .. num) == 0 then
      if player.dead then return end
      if player:getMark("sk__guhuo_cards") == 0 then
        mark = U.getUniversalCards(room, "bt")
        room:setPlayerMark(player, "sk__guhuo_cards", mark)
      end
      room:setPlayerMark(player, "sk__quanqing_target", target.id)
      local success, dat = room:askForUseActiveSkill(player, "sk__quanqing_active", "#sk__quanqing-choose::" .. target
      .id, false)
      room:setPlayerMark(player, "sk__quanqing_target", 0)
      if success and dat then
        local card = Fk:cloneCard(Fk:getCardById(dat.cards[1]).name)
        card.skillName = sk__quanqing.name
        room:useCard {
          from = target,
          tos = table.map(dat.targets, Util.Id2PlayerMapper),
          card = card,
        }
        if player:isWounded() and not player.dead then
          room:recover({
            who = player,
            num = 1,
            recoverBy = player,
            skillName = sk__quanqing.name,
          })
        end
      end
    end
  end,
})

return sk__quanqing
